﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.TubeDrawing
{
    public partial class ExtubationRiskNursingHistory : Form
    {
        #region 变量声明
        NursingFormRecord record = new NursingFormRecord();
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        PatientInfo Patientinfo;
        Tb_User LoginUser;
        #endregion

        #region 窗体构造函数
        public ExtubationRiskNursingHistory(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.Patientinfo = patientInfo;
            LoginUser = loginUser;
            BindControl();
            BindInfo();
        }
        #endregion

        #region GridView设置
        private void BindControl()
        {
            this.dateStart.DateTime = DateTime.Now.AddDays(-30);
            this.dateEnd.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 查询与加载
        private void BindInfo()
        {
            if (this.Patientinfo == null) return;
            string patientid = Patientinfo.PATIENT_ID;//住院号
            int VisitId = Patientinfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.Extubation;
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, this.dateStart.DateTime, this.dateEnd.DateTime);
            DataTable dt = new DataTable();
            dt.Columns.Add("PingGuRiQi", typeof(string));
            dt.Columns.Add("nianling1", typeof(string));
            dt.Columns.Add("nianling2", typeof(string));
            dt.Columns.Add("yishi1", typeof(string));
            dt.Columns.Add("yishi2", typeof(string));
            dt.Columns.Add("yishi3", typeof(string));
            dt.Columns.Add("naishouchengdu1", typeof(string));
            dt.Columns.Add("naishouchengdu2", typeof(string));
            dt.Columns.Add("naishouchengdu3", typeof(string));
            dt.Columns.Add("huodong1", typeof(string));
            dt.Columns.Add("huodong2", typeof(string));
            dt.Columns.Add("huodong3", typeof(string));
            dt.Columns.Add("huodong4", typeof(string));
            dt.Columns.Add("huodong5", typeof(string));
            dt.Columns.Add("hezuochengdu1", typeof(string));
            dt.Columns.Add("hezuochengdu2", typeof(string));
            dt.Columns.Add("hezuochengdu3", typeof(string));
            dt.Columns.Add("guandaogudingfangshi1", typeof(string));
            dt.Columns.Add("guandaogudingfangshi2", typeof(string));
            dt.Columns.Add("guandaogudingfangshi3", typeof(string));
            dt.Columns.Add("zongfen", typeof(string));
            dt.Columns.Add("gdzlgw", typeof(string));
            dt.Columns.Add("gdzlzw", typeof(string));
            dt.Columns.Add("gdzldw", typeof(string));
            dt.Columns.Add("hulicuoshi_pg", typeof(string));
            dt.Columns.Add("hulicuoshi_gd", typeof(string));
            dt.Columns.Add("hulicuoshi_zh", typeof(string));
            dt.Columns.Add("hulicuoshi_jy", typeof(string));
            dt.Columns.Add("hulicuoshi_ss", typeof(string));
            dt.Columns.Add("hulicuoshi_dw", typeof(string));
            dt.Columns.Add("hulicuoshi_js", typeof(string));
            dt.Columns.Add("hulicuoshi_gz", typeof(string));
            dt.Columns.Add("hulicuoshi_qita", typeof(string));
            dt.Columns.Add("YuFangXiaoGuo1", typeof(string));
            dt.Columns.Add("YuFangXiaoGuo2", typeof(string));
            dt.Columns.Add("suoyoudaoguanyibachu", typeof(string));
            dt.Columns.Add("shijian1", typeof(string));
            dt.Columns.Add("daiguanzhuanke", typeof(string));
            dt.Columns.Add("shijian2", typeof(string));
            dt.Columns.Add("daiguanchuyuan", typeof(string));
            dt.Columns.Add("shijian3", typeof(string));
            dt.Columns.Add("ZheRenHuShiQianMing", typeof(string));
            dt.Columns.Add("HuanZheQianMing", typeof(string));
            dt.Columns.Add("YuHuanZheGuanXi", typeof(string));
            dt.Columns.Add("yufangxiaoguo1", typeof(string));
            dt.Columns.Add("yufangxiaoguo2", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    Extubation bg = new Extubation();
                    bg = XmlUtil.XMLToObject<Extubation>(NursingFormData, Encoding.UTF8);

                    #region
                    DataRow newRow = dt.NewRow();

                    //年龄
                    int[] nianlinArray = { 2, 2 };
                    string nianling = bg.NianLin;
                    if (!string.IsNullOrEmpty(nianling))
                    {
                        int n = Convert.ToInt32(nianling);
                        newRow["nianling" + (n + 1)] = nianlinArray[n];
                    }

                    //意识
                    int[] yishiArray = { 3, 2, 1 };
                    string yishi = bg.YiShi;
                    if (!string.IsNullOrEmpty(yishi))
                    {
                        int n = Convert.ToInt32(yishi);
                        newRow["yishi" + (n + 1)] = yishiArray[n];
                    }

                    //耐受程度
                    int[] naishouchengduArray = { 3, 2, 1 };
                    string qingxu = bg.NaiShouChengDu;
                    if (!string.IsNullOrEmpty(qingxu))
                    {
                        int n = Convert.ToInt32(qingxu);
                        newRow["naishouchengdu" + (n + 1)] = naishouchengduArray[n];
                    }

                    //活动
                    string huodong = bg.HuoDong;
                    int[] huodongArray = { 2, 1 };
                    if (!string.IsNullOrEmpty(huodong) && Convert.ToInt32(huodong) < huodongArray.Length)
                    {
                        int n = Convert.ToInt32(huodong);
                        newRow["huodong" + (n + 1)] = huodongArray[n];
                    }

                    //合作程度
                    string hezuochengdu = bg.HeCuoChengDu;
                    int[] hezuochengduArray = { 3, 2, 1 };
                    if (!string.IsNullOrEmpty(hezuochengdu))
                    {
                        int n = Convert.ToInt32(hezuochengdu);
                        newRow["hezuochengdu" + (n + 1)] = hezuochengduArray[n];
                    }

                    //管道固定方式
                    int[] guandaogudingfangshiArray = { 3, 2, 1 };
                    string guandaogudingfangshi = bg.GuanDaoGuDingFangShi;
                    if (!string.IsNullOrEmpty(guandaogudingfangshi) && guandaogudingfangshi.Length == 1)
                    {
                        int n = Convert.ToInt32(guandaogudingfangshi);
                        newRow["guandaogudingfangshi" + (n + 1)] = guandaogudingfangshiArray[n];
                    }
                    if (!string.IsNullOrEmpty(guandaogudingfangshi) && guandaogudingfangshi.IndexOf('#') > 0 && guandaogudingfangshi.Split('#').Length == 3)
                    {
                        newRow["guandaogudingfangshi1"] = guandaogudingfangshi.Split('#')[0].Equals("1") ? guandaogudingfangshiArray[0].ToString() : string.Empty;
                        newRow["guandaogudingfangshi2"] = guandaogudingfangshi.Split('#')[1].Equals("1") ? guandaogudingfangshiArray[1].ToString() : string.Empty;
                        newRow["guandaogudingfangshi3"] = guandaogudingfangshi.Split('#')[2].Equals("1") ? guandaogudingfangshiArray[2].ToString() : string.Empty;
                    }

                    //管道种类-高危:高危里面的分数都是3分，GDZL_GaoWei_Text:存储数据使用/来存的，先分割出来统计分数再乘上3分就是总数。其他的中危(所有都是2分)、低危(所有都是1分)也是类似统计方式
                    string GDZL_GaoWei = bg.GDZL_GaoWei;
                    string[] GaoWeiArry = GDZL_GaoWei.Split('#');//例：0#0#0#0#0#0#0
                    string GDZL_GaoWei_Text = bg.GDZL_GaoWei_Text;
                    string[] GdArray = GDZL_GaoWei_Text.Split('/');//例：1/2/3/4/5/6/7
                    int gdzlgw = 0;
                    for (int i = 0; i < GdArray.Length; i++)
                    {
                        int eleName = Convert.ToInt32(GaoWeiArry[i]);//分割出来的值，如果为0说没选中checkbox,只填写了文本框内容
                        string val = GdArray[i];
                        if (eleName != 0 && !string.IsNullOrEmpty(val))//如果为0说没选中checkbox,只填写了文本框内容
                        {
                            gdzlgw += Convert.ToInt32(GdArray[i]);
                        }
                    }
                    newRow["gdzlgw"] = gdzlgw * 3;
                    //管道种类-中危
                    string GDZL_ZhongWei = bg.GDZL_ZhongWei;
                    string[] ZhongWeiArry = GDZL_ZhongWei.Split('#');//例：0#0#0#0#0#0#0
                    string GDZL_ZhongWei_Text = bg.GDZL_ZhongWei_Text;
                    string[] ZWArray = GDZL_ZhongWei_Text.Split('/');
                    int gdzlzw = 0;
                    for (int i = 0; i < ZWArray.Length; i++)
                    {
                        int eleName = Convert.ToInt32(ZhongWeiArry[i]);//分割出来的值，如果为0说没选中checkbox,只填写了文本框内容
                        string val = ZWArray[i];
                        if (eleName != 0 && !string.IsNullOrEmpty(val))//如果为0说没选中checkbox,只填写了文本框内容
                        {
                            gdzlzw += Convert.ToInt32(ZWArray[i]);
                        }
                    }
                    newRow["gdzlzw"] = gdzlzw * 2;
                    //管道种类-低危
                    string GDZL_DiWei = bg.GDZL_DiWei;
                    string[] DiWeiArry = GDZL_DiWei.Split('#');//例：0#0#0#0#0#0#0
                    string GDZL_DiWei_Text = bg.GDZL_DiWei_Text;
                    string[] DWArray = GDZL_DiWei_Text.Split('/');
                    int gdzldw = 0;
                    for (int i = 0; i < DWArray.Length; i++)
                    {
                        int eleName = Convert.ToInt32(DiWeiArry[i]);//分割出来的值，如果为0说没选中checkbox,只填写了文本框内容
                        string val = DWArray[i];
                        if (eleName != 0 && !string.IsNullOrEmpty(val))//如果为0说没选中checkbox,只填写了文本框内容
                        {
                            gdzldw += Convert.ToInt32(DWArray[i]);
                        }
                    }
                    newRow["gdzldw"] = gdzldw;
                    //护理措施
                    string HuLiCuoShi = bg.HuLiCuoShi;
                    string[] HuArray;
                    if (!string.IsNullOrEmpty(HuLiCuoShi))
                    {
                        HuArray = HuLiCuoShi.Split('#');
                        if (Convert.ToInt32(HuArray[0]) == 1)
                        {
                            newRow["hulicuoshi_pg"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[1]) == 1)
                        {
                            newRow["hulicuoshi_gd"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[2]) == 1)
                        {
                            newRow["hulicuoshi_zh"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[3]) == 1)
                        {
                            newRow["hulicuoshi_jy"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[4]) == 1)
                        {
                            newRow["hulicuoshi_ss"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[5]) == 1)
                        {
                            newRow["hulicuoshi_dw"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[6]) == 1)
                        {
                            newRow["hulicuoshi_js"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[7]) == 1)
                        {
                            newRow["hulicuoshi_gz"] = "√";
                        }
                        if (Convert.ToInt32(HuArray[8]) == 1)
                        {
                            newRow["hulicuoshi_qita"] = "√";
                        }
                    }
                    //无非计划
                    string YuFangXiaoGuo = bg.YuFangXiaoGuo;
                    if (!string.IsNullOrEmpty(YuFangXiaoGuo))
                    {
                        newRow["YuFangXiaoGuo" + (Convert.ToInt32(YuFangXiaoGuo) + 1)] = "√";
                    }

                    //总分
                    newRow["zongfen"] = bg.ZongFen;
                    //评估日期
                    string PingGuRiQi = bg.PingGuRiQi;
                    DateTime date = Convert.ToDateTime(PingGuRiQi);
                    newRow["PingGuRiQi"] = date.ToString("yyyy-MM-dd");
                    //责任护士签字
                    newRow["ZheRenHuShiQianMing"] = bg.ZheRenHuShiQianMing;

                    newRow["suoyoudaoguanyibachu"] = !string.IsNullOrEmpty(bg.SuoYouDaoGuanYiBaChu) && bg.SuoYouDaoGuanYiBaChu.Equals("1") ? "√" : "";
                    newRow["shijian1"] = bg.SuoYouDaoGuanYiBaChuShiJian;
                    newRow["daiguanzhuanke"] = !string.IsNullOrEmpty(bg.DaiGuanZhuanKe) && bg.DaiGuanZhuanKe.Equals("1") ? "√" : "";
                    newRow["shijian2"] = bg.DaiGuanZhuanKeShiJian;
                    newRow["daiguanchuyuan"] = !string.IsNullOrEmpty(bg.DaiGuanChuYuan) && bg.DaiGuanChuYuan.Equals("1") ? "√" : "";
                    newRow["shijian3"] = bg.DaiGuanChuYuanShiJian;

                    //隐藏ID触发
                    newRow["Id"] = record.Id;

                    dt.Rows.Add(newRow);
                    #endregion
                }
            }
            gridControl1.DataSource = dt;
        }


        #endregion

        #region 查询
        private void simpleButtonchaxun_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region GridView双击触发事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }
        #endregion

        #region 修改、新增
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            ShowEditForm();
        }

        private void ShowEditForm()
        {
            if (bandedGridView1.RowCount >= 1)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddExtubationRiskNursing addForm = new AddExtubationRiskNursing(LoginUser, Patientinfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        //新增
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddExtubationRiskNursing addForm = new AddExtubationRiskNursing(LoginUser, Patientinfo, "");
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 打印
        private void simpleButtondayin_Click(object sender, EventArgs e)
        {
            ExtubationRisPrint r = new ExtubationRisPrint(LoginUser, Patientinfo, dateStart.DateTime, dateEnd.DateTime);
            r.Show();
        }
        #endregion

        #region 删除
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    BindInfo();
                }
            }
        }
        #endregion
    }
}